/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lottoapp;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

/**
 *
 * @author mim
 */
public class SampleController implements Initializable {

    @FXML
    private TableView<Record> playerTable;
    @FXML
    private TableView<Record> remainingNumbersTable;
    @FXML
    private ListView<String> lottoResultTable;
    
    private ObservableList<Record> playerList = FXCollections.observableArrayList();
    private ObservableList<Record> remainingNumbersList = FXCollections.observableArrayList();
    @FXML
    private TableColumn nameCol;
    @FXML
    private TableColumn nr1;
    @FXML
    private TableColumn nr2;
    @FXML
    private TableColumn nr3;
    @FXML
    private TableColumn nr4;
    @FXML
    private TableColumn nr5;
    @FXML
    private TableColumn nr6;
    @FXML
    private TableColumn nr7;
    @FXML
    private TableColumn nr8;
    @FXML
    private TableColumn nr9;
    @FXML
    private TableColumn nr10;
    @FXML
    private TableColumn plays;
    @FXML
    private TableColumn nameColRemaining;
    @FXML
    private TableColumn nr1Remaining;
    @FXML
    private TableColumn nr2Remaining;
    @FXML
    private TableColumn nr3Remaining;
    @FXML
    private TableColumn nr4Remaining;
    @FXML
    private TableColumn nr5Remaining;
    @FXML
    private TableColumn nr6Remaining;
    @FXML
    private TableColumn nr7Remaining;
    @FXML
    private TableColumn nr8Remaining;
    @FXML
    private TableColumn nr9Remaining;
    @FXML
    private TableColumn nr10Remaining;

    @FXML
    public void handleButtonAction(ActionEvent event) throws Exception {
        try {
            LottoResult lottoResult = LottoFetcher.fetchLotto();
            LottoAppModel.getInstance().addResult(lottoResult);
            LottoAppModel.save();
            loadDataToModel();            
            playerTable.setItems(null);
            playerTable.setItems(playerList);
            remainingNumbersTable.setItems(null);
            remainingNumbersTable.setItems(remainingNumbersList);
        } catch (IOException ex) {
            Logger.getLogger(SampleController.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        initPlayerTable();
        initRemainingNumbersTable();
        LottoAppModel.load();
        loadDataToModel();
        playerTable.setItems(playerList);
        remainingNumbersTable.setItems(remainingNumbersList);
    }

    private void initPlayerTable() {
        nameCol.setCellValueFactory(
                new PropertyValueFactory<Record, String>("name"));
        nr1.setCellValueFactory(
                new PropertyValueFactory<Record, Integer>("nr1"));
        nr2.setCellValueFactory(
                new PropertyValueFactory<Record, Integer>("nr2"));
        nr3.setCellValueFactory(
                new PropertyValueFactory<Record, Integer>("nr3"));
        nr4.setCellValueFactory(
                new PropertyValueFactory<Record, Integer>("nr4"));
        nr5.setCellValueFactory(
                new PropertyValueFactory<Record, Integer>("nr5"));
        nr6.setCellValueFactory(
                new PropertyValueFactory<Record, Integer>("nr6"));
        nr7.setCellValueFactory(
                new PropertyValueFactory<Record, Integer>("nr7"));
        nr8.setCellValueFactory(
                new PropertyValueFactory<Record, Integer>("nr8"));
        nr9.setCellValueFactory(
                new PropertyValueFactory<Record, Integer>("nr9"));
        nr10.setCellValueFactory(
                new PropertyValueFactory<Record, Integer>("nr10"));
        plays.setCellValueFactory(
                new PropertyValueFactory<Record, Boolean>("plays"));

    }

    private void initRemainingNumbersTable() {
        nameColRemaining.setCellValueFactory(
                new PropertyValueFactory<Record, String>("name"));
        nr1Remaining.setCellValueFactory(
                new PropertyValueFactory<Record, Integer>("nr1"));
        nr2Remaining.setCellValueFactory(
                new PropertyValueFactory<Record, Integer>("nr2"));
        nr3Remaining.setCellValueFactory(
                new PropertyValueFactory<Record, Integer>("nr3"));
        nr4Remaining.setCellValueFactory(
                new PropertyValueFactory<Record, Integer>("nr4"));
        nr5Remaining.setCellValueFactory(
                new PropertyValueFactory<Record, Integer>("nr5"));
        nr6Remaining.setCellValueFactory(
                new PropertyValueFactory<Record, Integer>("nr6"));
        nr7Remaining.setCellValueFactory(
                new PropertyValueFactory<Record, Integer>("nr7"));
        nr8Remaining.setCellValueFactory(
                new PropertyValueFactory<Record, Integer>("nr8"));
        nr9Remaining.setCellValueFactory(
                new PropertyValueFactory<Record, Integer>("nr9"));
        nr10Remaining.setCellValueFactory(
                new PropertyValueFactory<Record, Integer>("nr10"));

    }

    private void loadDataToModel() {

        LottoAppModel model = LottoAppModel.getInstance();
        Set<LottoResult> results = model.getResult();
        lottoResultTable.getItems().clear();
        for(LottoResult lottoResult:results){
            final String lottoResultString = lottoResult.toString();
            //System.out.println("lotto:"+lottoResultString);
            lottoResultTable.getItems().add(lottoResultString);
        }
        playerList.clear();
        remainingNumbersList.clear();
        for (Player player : model.getPlayers()) {
            Record playerRecord = Record.fromPlayer(player);
            playerList.add(playerRecord);
            Set<Integer> remainingNumbers = player.remainingNumbersAfterProcessing(results);
            Record remaining = Record.fromPlayerWithResults(player, remainingNumbers);
            remainingNumbersList.add(remaining);
        }
    }
}
